
export const Observer = {
  list:{},
  cacheData :{},
  subscribe(key, fun) {
    if (!this.list[key]) 
      this.list[key] = []
      
    this.list[key].push(fun)
  },
  publish(key, ...params) {
    const fns = this.list[key]
    if (!fns || fns.length === 0) 
      return false
    for (let i = 0;i < fns.length; i++ ) 
      fns[i](...params)
  },
  setCache(key, data) {
    this.cacheData[key] = data
  },
  getCache(key) {
    return this.cacheData[key]
  },
  remove(key, fn) {
    const fns = this.list[key]
    if (!fns)
      return false
    if (!fn) {
      fns && (fns.length = 0)
    }
    else {
      for (let l = fns.length - 1; l >= 0; l--) {
        const _fn = fns[l]
        if (_fn === fn) {
          fns.splice(l, 1)
          break
        }
      }
    }
  },
}
   
export  function typeOf(data, type) {
  const typestr = Object.prototype.toString.call(data).split(' ')[1].slice(0, -1).toLowerCase()
  return type == undefined ? typestr : Array.isArray(type) ? type.includes(typestr) : typestr == type
}
